package com.tanhua.api.mongo;

import com.tanhua.domain.mongo.Visitors;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class VisitorsApiImpl implements VisitorsApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Visitors> topVisitors(Long userId, Long historyDate) {

        // 1、拼接条件
        Query query = Query.query(
                Criteria.where("userId").is(userId)
                        .and("date").gt(historyDate)
        ).with(Sort.by(Sort.Order.desc("date")))
                .limit(5);
        // 2、查询
        return mongoTemplate.find(query, Visitors.class);
    }
}
